﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace wp8skinswitcher.Model
{
    public class PhoneSkinService : IPhoneSkinService
    {
        public void GetPhoneSkins(Action<IEnumerable<PhoneSkin>, Exception> callback)
        {
            List<PhoneSkin> skins = new List<PhoneSkin>();
            
            #region 480
            
            var default480 = new PhoneSkin("Default");
            default480.Id = "WM8";
            default480.X = 37;
            default480.Y = 73;
            default480.Type = EmulatorType.wvga;
            skins.Add(default480);

            var nokialumia820red = new PhoneSkin("Nokia Lumia 820");
            nokialumia820red.Id = "Nokia_Lumia_820_Red";
            nokialumia820red.CreatorLink = "http://www.vdcruijsen.net";
            nokialumia820red.CreatorName = "Geert van der Cruijsen";
            nokialumia820red.X = 79;
            nokialumia820red.Y = 136;
            nokialumia820red.SubTitle = "Red";
            nokialumia820red.Type = EmulatorType.wvga;
            skins.Add(nokialumia820red);

            var nokialumia820yellow = new PhoneSkin("Nokia Lumia 820");
            nokialumia820yellow.Id = "Nokia_Lumia_820_Yellow";
            nokialumia820yellow.CreatorLink = "http://www.vdcruijsen.net";
            nokialumia820yellow.CreatorName = "Geert van der Cruijsen";
            nokialumia820yellow.X = 77;
            nokialumia820yellow.Y = 131;
            nokialumia820yellow.SubTitle = "Yellow";
            nokialumia820yellow.Type = EmulatorType.wvga;
            skins.Add(nokialumia820yellow);

            var nokialumia820black = new PhoneSkin("Nokia Lumia 820");
            nokialumia820black.Id = "Nokia_Lumia_820_Black";
            nokialumia820black.CreatorLink = "http://www.vdcruijsen.net";
            nokialumia820black.CreatorName = "Geert van der Cruijsen";
            nokialumia820black.X = 70;
            nokialumia820black.Y = 106;
            nokialumia820black.SubTitle = "Black";
            nokialumia820black.Type = EmulatorType.wvga;
            skins.Add(nokialumia820black);

            var nokialumia820cyan = new PhoneSkin("Nokia Lumia 820");
            nokialumia820cyan.Id = "Nokia_Lumia_820_Cyan";
            nokialumia820cyan.CreatorLink = "http://www.vdcruijsen.net";
            nokialumia820cyan.CreatorName = "Geert van der Cruijsen";
            nokialumia820cyan.X = 68;
            nokialumia820cyan.Y = 105;
            nokialumia820cyan.SubTitle = "Cyan";
            nokialumia820cyan.Type = EmulatorType.wvga;
            skins.Add(nokialumia820cyan);

            var nokialumia820white = new PhoneSkin("Nokia Lumia 820");
            nokialumia820white.Id = "Nokia_Lumia_820_White";
            nokialumia820white.CreatorLink = "http://www.vdcruijsen.net";
            nokialumia820white.CreatorName = "Geert van der Cruijsen";
            nokialumia820white.X = 69;
            nokialumia820white.Y = 107;
            nokialumia820white.SubTitle = "White";
            nokialumia820white.Type = EmulatorType.wvga;
            skins.Add(nokialumia820white);

            var htc8sBlue = new PhoneSkin("HTC 8s");
            htc8sBlue.Id = "HTC_8S_Blue";
            htc8sBlue.CreatorLink = "http://www.vdcruijsen.net";
            htc8sBlue.CreatorName = "Geert van der Cruijsen";
            htc8sBlue.X = 72;
            htc8sBlue.Y = 152;
            htc8sBlue.SubTitle = "Blue";
            htc8sBlue.Type = EmulatorType.wvga;
            skins.Add(htc8sBlue);

            var htc8sOrange = new PhoneSkin("HTC 8s");
            htc8sOrange.Id = "HTC_8S_Orange";
            htc8sOrange.CreatorLink = "http://www.vdcruijsen.net";
            htc8sOrange.CreatorName = "Geert van der Cruijsen";
            htc8sOrange.X = 76;
            htc8sOrange.Y = 147;
            htc8sOrange.SubTitle = "Orange";
            htc8sOrange.Type = EmulatorType.wvga;
            skins.Add(htc8sOrange);

            #endregion

            #region 720p
            
            var default720 = new PhoneSkin("Default");
            default720.Id = "WM8";
            default720.X = 62;
            default720.Y = 120;
            default720.Type = EmulatorType.s720p;
            skins.Add(default720);

            var samsungAtivS = new PhoneSkin("Samsung Ativ S");
            samsungAtivS.Id = "Samsung_ATIV_S";
            samsungAtivS.CreatorLink = "http://www.vdcruijsen.net";
            samsungAtivS.CreatorName = "Geert van der Cruijsen";
            samsungAtivS.X = 71;
            samsungAtivS.Y = 176;
            samsungAtivS.SubTitle = "";
            samsungAtivS.Type = EmulatorType.s720p;
            skins.Add(samsungAtivS);

            var htc8xblack = new PhoneSkin("HTC 8X");
            htc8xblack.Id = "HTC_8X_Black";
            htc8xblack.CreatorLink = "http://www.vdcruijsen.net";
            htc8xblack.CreatorName = "Geert van der Cruijsen";
            htc8xblack.X = 93;
            htc8xblack.Y = 226;
            htc8xblack.SubTitle = "Black";
            htc8xblack.Type = EmulatorType.s720p;
            skins.Add(htc8xblack);

            var htc8xblue = new PhoneSkin("HTC 8X");
            htc8xblue.Id = "HTC_8X_Blue";
            htc8xblue.CreatorLink = "http://www.vdcruijsen.net";
            htc8xblue.CreatorName = "Geert van der Cruijsen";
            htc8xblue.X = 93;
            htc8xblue.Y = 242;
            htc8xblue.SubTitle = "Blue";
            htc8xblue.Type = EmulatorType.s720p;
            skins.Add(htc8xblue);

            var htc8xNeon = new PhoneSkin("HTC 8X");
            htc8xNeon.Id = "HTC_8X_Neon";
            htc8xNeon.CreatorLink = "http://www.vdcruijsen.net";
            htc8xNeon.CreatorName = "Geert van der Cruijsen";
            htc8xNeon.X = 91;
            htc8xNeon.Y = 228;
            htc8xNeon.SubTitle = "Neon";
            htc8xNeon.Type = EmulatorType.s720p;
            skins.Add(htc8xNeon);

            var htc8xRed = new PhoneSkin("HTC 8X");
            htc8xRed.Id = "HTC_8X_Red";
            htc8xRed.CreatorLink = "http://www.vdcruijsen.net";
            htc8xRed.CreatorName = "Geert van der Cruijsen";
            htc8xRed.X = 94;
            htc8xRed.Y = 231;
            htc8xRed.SubTitle = "Red";
            htc8xRed.Type = EmulatorType.s720p;
            skins.Add(htc8xRed);
            
            #endregion

            #region 768
            var default768 = new PhoneSkin("Default");
            default768.Id = "WM8";
            default768.X = 62;
            default768.Y = 120;
            default768.Type = EmulatorType.wxga;
            skins.Add(default768);

            var lumia920yellow = new PhoneSkin("Nokia Lumia 920");
            lumia920yellow.Id = "Nokia_Lumia_920_Yellow";
            lumia920yellow.CreatorLink = "http://www.vdcruijsen.net";
            lumia920yellow.CreatorName = "Geert van der Cruijsen";
            lumia920yellow.X = 150;
            lumia920yellow.Y = 219;
            lumia920yellow.SubTitle = "Yellow";
            lumia920yellow.Type = EmulatorType.wxga;
            skins.Add(lumia920yellow);

            var lumia920red = new PhoneSkin("Nokia Lumia 920");
            lumia920red.Id = "Nokia_Lumia_920_Red";
            lumia920red.CreatorLink = "http://www.vdcruijsen.net";
            lumia920red.CreatorName = "Geert van der Cruijsen";
            lumia920red.X = 147;
            lumia920red.Y = 210;
            lumia920red.SubTitle = "Red";
            lumia920red.Type = EmulatorType.wxga;
            skins.Add(lumia920red);

            var lumia920black = new PhoneSkin("Nokia Lumia 920");
            lumia920black.Id = "Nokia_Lumia_920_Black";
            lumia920black.CreatorLink = "http://www.vdcruijsen.net";
            lumia920black.CreatorName = "Geert van der Cruijsen";
            lumia920black.X = 151;
            lumia920black.Y = 211;
            lumia920black.SubTitle = "Black";
            lumia920black.Type = EmulatorType.wxga;
            skins.Add(lumia920black);

            var lumia920grey = new PhoneSkin("Nokia Lumia 920");
            lumia920grey.Id = "Nokia_Lumia_920_Grey";
            lumia920grey.CreatorLink = "http://www.vdcruijsen.net";
            lumia920grey.CreatorName = "Geert van der Cruijsen";
            lumia920grey.X = 154;
            lumia920grey.Y = 210;
            lumia920grey.SubTitle = "Grey";
            lumia920grey.Type = EmulatorType.wxga;
            skins.Add(lumia920grey);

            var lumia920white = new PhoneSkin("Nokia Lumia 920");
            lumia920white.Id = "Nokia_Lumia_920_White";
            lumia920white.CreatorLink = "http://www.vdcruijsen.net";
            lumia920white.CreatorName = "Geert van der Cruijsen";
            lumia920white.X = 153;
            lumia920white.Y = 225;
            lumia920white.SubTitle = "White";
            lumia920white.Type = EmulatorType.wxga;
            skins.Add(lumia920white);

            #endregion

            callback(skins.OrderBy(s => s.Type).ThenBy(s => s.Title).ThenBy(s => s.SubTitle), null);
        }

        public bool SetEmulatorPhoneSkin(PhoneSkin skin)
        {
            XmlDocument xdoc = new XmlDocument();

            string programfiles = "";
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                programfiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }
            else
            {
                programfiles = Environment.GetEnvironmentVariable("ProgramFiles");
            }

            switch (skin.Type)
            {
                case EmulatorType.wvga:
                default:
                    xdoc.Load(programfiles + "\\Microsoft XDE\\8.0\\WM8_Skin_480x800.xml");
                    break;
                case EmulatorType.s720p:
                    xdoc.Load(programfiles + "\\Microsoft XDE\\8.0\\WM8_Skin_720x1280.xml");
                    break;
                case EmulatorType.wxga:
                    xdoc.Load(programfiles + "\\Microsoft XDE\\8.0\\WM8_Skin_768x1280.xml");
                    break;
            }
            
            XmlNode view = xdoc.DocumentElement.SelectSingleNode("Display");
            view.Attributes["DownImage"].Value = skin.Id + "_Skin_Down_" + skin.GetResolutionString() + ".png";
            view.Attributes["NormalImage"].Value = skin.Id + "_Skin_Up_" + skin.GetResolutionString() + ".png";
            view.Attributes["MappingImage"].Value = skin.Id + "_Skin_Mask_" + skin.GetResolutionString() + ".png";
            view.Attributes["DisplayPosX"].Value = skin.X.ToString();
            view.Attributes["DisplayPosY"].Value = skin.Y.ToString();


            
            XmlNode xbuttons = xdoc.DocumentElement.SelectSingleNode("//Buttons");

            XmlNodeList buttons = xbuttons.SelectNodes("Button");
            for (int i = buttons.Count - 1; i >= 0; i--)
            {
                xbuttons.RemoveChild(buttons[i]);

            }

            //<button mappingColor="0xF7941D" onPressAndHold="0x3B" toolTip="Back"/> 
            XmlNode button1 = xdoc.CreateElement("Button");
            ((XmlElement)button1).SetAttribute("MappingColor", "0xF7941D");
            ((XmlElement)button1).SetAttribute("OnClick", "0x1B");
            ((XmlElement)button1).SetAttribute("ToolTip", "Back");
            xbuttons.AppendChild(button1);

            //<button mappingColor="0x66CC66" onPressAndHold="0x3c" toolTip="Start"/> 
            XmlNode button2 = xdoc.CreateElement("Button");
            ((XmlElement)button2).SetAttribute("MappingColor", "0x66CC66");
            ((XmlElement)button2).SetAttribute("OnClick", "0x71");
            ((XmlElement)button2).SetAttribute("ToolTip", "Start");
            xbuttons.AppendChild(button2);

            //<button mappingColor="0xA0410D" onPressAndHold="0x3D" toolTip="Search"/>
            XmlNode button3 = xdoc.CreateElement("Button");
            ((XmlElement)button3).SetAttribute("MappingColor", "0xA0410D");
            ((XmlElement)button3).SetAttribute("OnClick", "0x72");
            ((XmlElement)button3).SetAttribute("ToolTip", "Search");
            xbuttons.AppendChild(button3);

            //<button mappingColor="0xA0A30D" onPressAndHold="Key_F9" toolTip="Volume Up"/>
            //XmlNode button4 = xdoc.CreateElement("Button");
            //((XmlElement)button4).SetAttribute("MappingColor", "0xA0A30D");
            //((XmlElement)button4).SetAttribute("OnClick", "Key_F9");
            //((XmlElement)button4).SetAttribute("ToolTip", "Volume Up");
            //xbuttons.AppendChild(button4);

            ////<button mappingColor="0xDD6500" onPressAndHold="Key_F10" toolTip="Volume Down"/>
            //XmlNode button5 = xdoc.CreateElement("Button");
            //((XmlElement)button5).SetAttribute("MappingColor", "0xDD6500");
            //((XmlElement)button5).SetAttribute("OnClick", "Key_F10");
            //((XmlElement)button5).SetAttribute("ToolTip", "Volume Down");
            //xbuttons.AppendChild(button5);

            ////<button mappingColor="0x3EAA00" onPressAndHold="SHUTDOWN" toolTip="Power"/> 
            //XmlNode button6 = xdoc.CreateElement("Button");
            //((XmlElement)button6).SetAttribute("MappingColor", "0x3EAA00");
            //((XmlElement)button6).SetAttribute("OnClick", "SHUTDOWN");
            //((XmlElement)button6).SetAttribute("ToolTip", "Power");
            //xbuttons.AppendChild(button6);

            ////<button mappingColor="0x3EAAC6" onPressAndHold="Key_F7" toolTip="Camera"/>
            //XmlNode button7 = xdoc.CreateElement("Button");
            //((XmlElement)button7).SetAttribute("MappingColor", "0x3EAAC6");
            //((XmlElement)button7).SetAttribute("OnClick", "Key_F7");
            //((XmlElement)button7).SetAttribute("ToolTip", "Camera");
            //xbuttons.AppendChild(button7);


            switch (skin.Type)
            {
                case EmulatorType.wvga:
                default:
                    xdoc.Save(programfiles + "\\Microsoft XDE\\8.0\\WM8_Skin_480x800.xml");
                    break;
                case EmulatorType.s720p:
                    xdoc.Save(programfiles + "\\Microsoft XDE\\8.0\\WM8_Skin_720x1280.xml");
                    break;
                case EmulatorType.wxga:
                    xdoc.Save(programfiles + "\\Microsoft XDE\\8.0\\WM8_Skin_768x1280.xml");
                    break;
            }
            return true;
        }
    }
}
